package com.hn658.base.message.notification.msg.business.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hn658.base.message.group.business.IGroupManager;
import com.hn658.base.message.group.exception.GroupException;
import com.hn658.base.message.notification.exception.NotificationException;
import com.hn658.base.message.notification.msg.business.IMessageManager;
import com.hn658.base.message.notification.msg.dao.mongo.IMessageMongoDBDao;
import com.hn658.base.message.notification.msg.dto.MsgQueryDTO;
import com.hn658.base.message.notification.msg.entity.MessageEO;
import com.hn658.framework.dataaccess.pagination.GenericPage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by baihai on 2015/11/26.
 */
@Component
public class MessageManagerImpl implements IMessageManager {


    @Autowired
    private IMessageMongoDBDao messageMongoDBDao;

    @Autowired
    private IGroupManager groupManager;

    @Override
    public MessageEO save(MessageEO messageEO){

        Long id = messageMongoDBDao.getMaxId();

        messageEO.setId(id+1);
        messageMongoDBDao.insert(messageEO);

        return messageEO;

    }



	@Override
	public List<MessageEO> queryList(Map<String, Object> queryParam) {
		return messageMongoDBDao.queryList(queryParam);
	}




	/**
	 * 查询私聊消息记录
	 * @param query
	 * @return
	 */
	@Override
	public GenericPage<MessageEO> querySinglePage(MsgQueryDTO query){
		Map<String, Object> queryMap = this.initParam(query);
		if(query.getToUid()==null){
			throw new NotificationException(NotificationException.EmptyObjectIdError);
		}
		queryMap.remove("toGroupId");
		return queryPage(queryMap, query.getPageSize(), query.getStart(), query.getOrderBy(), query.isAsc());
	}
	
	/**
	 * 查询群消息记录
	 * @param query
	 * @return
	 */
	@Override
	public GenericPage<MessageEO> queryGroupPage(MsgQueryDTO query){
		Map<String, Object> queryMap = this.initParam(query);
		if(query.getToGroupId()==null){
			throw new GroupException(GroupException.EmptyGroupId);
		}
		boolean allow = groupManager.isGroupMember(query.getAppId(), query.getToGroupId(), query.getFromUid());
		if(!allow){
			throw new GroupException(GroupException.GroupMemberNoExist);
		}
		queryMap.remove("toUid");
		queryMap.remove("uid");
		return queryPage(queryMap, query.getPageSize(), query.getStart(), query.getOrderBy(), query.isAsc());
	}
	
	/**
	 * 查询聊天记录
	 * @param queryParam
	 * @param pageSize
	 * @param start
	 * @param orderBy
	 * @param isAsc
	 * @return
	 */
	private GenericPage<MessageEO> queryPage(Map<String, Object> queryParam,
			int pageSize, int start, String orderBy, boolean isAsc) {
		return messageMongoDBDao.queryPage(queryParam, pageSize, start, orderBy, isAsc);
	}
	
	/**
	 * 初始化mongo查询map
	 * @param query
	 * @return
	 */
	private Map<String, Object> initParam(MsgQueryDTO query){
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if(StringUtils.isBlank(query.getAppId())){
			throw new NotificationException(NotificationException.EmptyAppIdError);
		}
		queryMap.put("appId", query.getAppId());
		if(query.getFromUid()==null){
			throw new NotificationException(NotificationException.EmptyUidError);
		}
		queryMap.put("uid", query.getFromUid());
		if(query.getToGroupId()!=null){
			queryMap.put("toGroupId", query.getToGroupId());
		}
		if(query.getToUid()!=null){
			queryMap.put("toUid", query.getToUid());
		}
		return queryMap;
	}
}
